#ifndef MemDC_h__
#define MemDC_h__
#include <windows.h>
class CMemDC
{
public:
    CMemDC(HDC&pDC)
    {
        m_pDC = pDC;
        m_OldBitmap = NULL;           
		::GetClipBox(pDC,&m_rect);
		if ((m_memDC=::CreateCompatibleDC(pDC))!=NULL)
		{
			if ((m_bitmap=::CreateCompatibleBitmap(pDC,m_rect.right-m_rect.left,m_rect.bottom-m_rect.top))!=NULL)
			{ 		
				m_OldBitmap = (HBITMAP)::SelectObject(m_memDC,m_bitmap);
				::SetWindowOrgEx(m_memDC,m_rect.left,m_rect.top,NULL);
				::FillRect(m_memDC,&m_rect,WHITE_BRUSH);
			}       
		}
    }
	CMemDC(HDC&pDC,HBITMAP&bit)
	{
		m_pDC=pDC;
		m_OldBitmap=NULL;
		::GetClipBox(pDC,&m_rect);
		if ((m_memDC=::CreateCompatibleDC(pDC))!=NULL)
		{
			m_bitmap=bit;
			m_OldBitmap=(HBITMAP)::SelectObject(m_memDC,m_bitmap);
			::SetWindowOrgEx(m_memDC,m_rect.left,m_rect.top,NULL);
		}
	}
    ~CMemDC()
    {
// 		::BitBlt(m_pDC,m_rect.left,m_rect.top,m_rect.right-m_rect.left,m_rect.bottom-m_rect.top,m_memDC,m_rect.left,m_rect.top,SRCCOPY);
// 		::SelectObject(m_memDC,m_OldBitmap);
    }
	void Render()
	{
		::BitBlt(m_pDC,m_rect.left,m_rect.top,m_rect.right-m_rect.left,m_rect.bottom-m_rect.top,m_memDC,0,0,SRCCOPY);
		::SelectObject(m_memDC,m_OldBitmap);
		::DeleteObject(m_bitmap);
		::DeleteDC(m_memDC);
	}
    operator HDC*() {return &m_memDC;}
	operator HDC(){return m_memDC;}
private:
    HBITMAP  m_bitmap;
    HBITMAP m_OldBitmap;  
    HDC     m_pDC;   
	HDC		 m_memDC;      
    RECT    m_rect;            
};
#endif // MemDC_h__